/**
 * 项目中使用的图标，可按需加载
 * 支持，从URL中获取，支持Iconfont，支持本地图标等。
 */
import React from 'react';
import Icon from '@ant-design/icons';
import * as Icons from '@ant-design/icons';
import { CpasIcon } from '~/public/cpas-ui';

// 参考 https://blog.csdn.net/xhwnous/article/details/90478979
const isUrl = (path: string): boolean => {
  if (!path.startsWith('http')) {
    return false;
  }
  try {
    const url = new URL(path);
    return !!url;
  } catch (error) {
    return false;
  }
};
/** 判断是否是图片链接 */
function isImg(path: string): boolean {
  return /\w.(png|jpg|jpeg|svg|webp|gif|bmp)$/i.test(path);
}

// Allow menu.js config icon as string or ReactNode
//   icon: 'setting',
//   icon: 'icon-geren' #For Iconfont ,
//   icon: 'http://demo.com/icon.png',
//   icon: '/favicon.png',
//   icon: <Icon type="setting" />,
const getIcon = (icon?: string | React.ReactNode): React.ReactNode => {
  if (typeof icon === 'string' && icon && icon.includes('-')) {
    if (icon === 'icon-tuoyuanxing1') {
      return <CpasIcon fontSize={4} type={`${icon}`} />
    }
    return <CpasIcon type={`${icon}`} />
  }
  if (typeof icon === 'string' && icon !== '') {
    if (isUrl(icon) || isImg(icon)) {
      return (
        <Icon component={() => <img src={icon} alt="icon" className="ant-pro-sider-menu-icon" />} />
      );
    }
    const NewIcon = Icons[icon] || Icons[`${icon}Outlined`];
    if (NewIcon) {
      try {
        return React.createElement(NewIcon);
      } catch (error) {
        return React.createElement(Icons.FileOutlined); // 默认
      }
    }


  }
  // <CpasIcon type='icon-er-digaomoban'/>
  return icon;
};

/**
 * 根据文件扩展名获取文件图标
 * @param fileExt 文件扩展名
 * @returns
 */
const getFileIcon = (fileExt?: string): React.ReactNode => {
  const FileExtEnum = {
    xlsx: 'FileExcelOutlined',
    xls: 'FileExcelOutlined',
    doc: 'FileWordOutlined',
    docx: 'FileWordOutlined',
    jpg: 'FilejpgOutlined',
    jpeg: 'FilejpgOutlined',
  };
  let newFileExt = fileExt || 'File';
  if (newFileExt?.startsWith('.')) {
    newFileExt = newFileExt.substr(1);
  }
  const NewIcon = Icons[FileExtEnum[newFileExt]] || Icons.FileOutlined;
  if (NewIcon) {
    try {
      return React.createElement(NewIcon);
    } catch (error) {
      return React.createElement(Icons.FileOutlined); // 默认
    }
  }
  return React.createElement(Icons.FileOutlined);
};

/**
 * 根据文件扩展名获取文件图标
 * @param fileExt 文件扩展名
 * @returns
 */
//  xlsx,xls,docx,doc,jpg,gif,png,txt,pdf,rar,rar4,zip,mp3,mp4
const getCpasIcon = (fileExt?: string, iconSize: number = 16) => {
  let iconName = '';
  if (fileExt === ".xlsx" || fileExt === ".xlsd" || fileExt === ".xls") {
    iconName = 'icon-a-XLSXXLS';
  } else if (fileExt === ".pdf") {
    iconName = 'icon-PDF';
  } else if (fileExt === ".txt") {
    iconName = 'icon-TXT';
  } else if (fileExt === ".ppt") {
    iconName = 'icon-PPT';
  } else if (fileExt === ".png") {
    iconName = 'icon-PNG';
  } else if (fileExt === ".jpg") {
    iconName = 'icon-JPG';
  } else if (fileExt === ".rar" || fileExt === ".rar4") {
    iconName = 'icon-a-RARRAR4';
  } else if (fileExt === ".MP4") {
    iconName = 'icon-MP4';
  } else if (fileExt === ".MP3") {
    iconName = 'icon-MP3';
  } else if (fileExt === ".docx" || fileExt === ".doc") {
    iconName = 'icon-a-DOCXDOC';
  } else if (fileExt === ".gif") {
    iconName = 'icon-GIF';
  } else if (fileExt === ".word") {
    iconName = 'icon-WORD';
  } else if (fileExt === ".zip") {
    iconName = 'icon-ZIP';
  } else if (fileExt === "." || fileExt === "..") {
    iconName = 'icon-wenjianjia';
  } else {
    iconName = 'icon-qita';
  }
  return <CpasIcon type={iconName} fontSize={iconSize} />
}

export default getIcon;
export { getFileIcon, isUrl, isImg, Icons, getCpasIcon };
